<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Denzel - HTML5.2新标签 —— dialog </title>
    <meta name="description" content="前端笔记">
    <meta name="keywords" content="web前端,HTML5,CSS3,Javascript,SVG,canvas">
  <meta name="anthor" content="Denzel">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
  <meta name="msapplication-TileColor" content="#000000">
  <link rel="icon" href="/blog/vuepress/logo.png">
  <link rel="manifest" href="/blog/vuepress/manifest.json">
  <link rel="apple-touch-icon" href="/blog/vuepress/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="/blog/vuepress/icons/safari-pinned-tab.svg" color="#3eaf7c">
    
    <link rel="preload" href="/blog/vuepress/assets/css/0.styles.d4f29607.css" as="style"><link rel="preload" href="/blog/vuepress/assets/js/app.15770e34.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/41.e1e0a3bf.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/78.02cdd59d.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/77.469aed17.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/76.0916a3cd.js" as="script"><link rel="prefetch" href="/blog/vuepress/assets/js/46.59c0ece7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/1.05e4270f.js"><link rel="prefetch" href="/blog/vuepress/assets/js/2.ace739f7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/3.636e5a79.js"><link rel="prefetch" href="/blog/vuepress/assets/js/4.7a55aa4b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/5.b56b0573.js"><link rel="prefetch" href="/blog/vuepress/assets/js/6.e9a3fbc8.js"><link rel="prefetch" href="/blog/vuepress/assets/js/7.aa46c47d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/8.a9288ac6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/9.b9f8dec6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/10.3e2ea470.js"><link rel="prefetch" href="/blog/vuepress/assets/js/11.4d115f98.js"><link rel="prefetch" href="/blog/vuepress/assets/js/12.c97cf4d0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/13.1e0cf594.js"><link rel="prefetch" href="/blog/vuepress/assets/js/14.bab9026a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/15.1a24cffe.js"><link rel="prefetch" href="/blog/vuepress/assets/js/16.e9995850.js"><link rel="prefetch" href="/blog/vuepress/assets/js/17.86a1f7d2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/18.c32400c4.js"><link rel="prefetch" href="/blog/vuepress/assets/js/19.9f99887c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/20.82f0c2f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/21.7f5c223d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/22.acf9934c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/23.20d8e817.js"><link rel="prefetch" href="/blog/vuepress/assets/js/24.acf27379.js"><link rel="prefetch" href="/blog/vuepress/assets/js/25.20165510.js"><link rel="prefetch" href="/blog/vuepress/assets/js/26.83a2ddc3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/27.33276a04.js"><link rel="prefetch" href="/blog/vuepress/assets/js/28.e320efab.js"><link rel="prefetch" href="/blog/vuepress/assets/js/29.3fd92c30.js"><link rel="prefetch" href="/blog/vuepress/assets/js/30.2bc84d43.js"><link rel="prefetch" href="/blog/vuepress/assets/js/31.d70ed500.js"><link rel="prefetch" href="/blog/vuepress/assets/js/32.e7a0f8e1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/33.c48ee70b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/34.8ad03d1b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/35.5bdb3994.js"><link rel="prefetch" href="/blog/vuepress/assets/js/36.a25f926e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/37.7515cfbb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/38.99347371.js"><link rel="prefetch" href="/blog/vuepress/assets/js/39.3481fc62.js"><link rel="prefetch" href="/blog/vuepress/assets/js/40.bc709734.js"><link rel="prefetch" href="/blog/vuepress/assets/js/42.762b61b0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/43.8f0b2d6c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/44.2a0a4ee0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/45.baf7ed60.js"><link rel="prefetch" href="/blog/vuepress/assets/js/47.11a8720a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/48.e0bf8b54.js"><link rel="prefetch" href="/blog/vuepress/assets/js/49.c96c4011.js"><link rel="prefetch" href="/blog/vuepress/assets/js/50.31189263.js"><link rel="prefetch" href="/blog/vuepress/assets/js/51.766a8294.js"><link rel="prefetch" href="/blog/vuepress/assets/js/52.26b971fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/53.0cfbd708.js"><link rel="prefetch" href="/blog/vuepress/assets/js/54.bdc15c87.js"><link rel="prefetch" href="/blog/vuepress/assets/js/55.fea285a9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/56.e030a6b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/57.bd168a1e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/58.636557fa.js"><link rel="prefetch" href="/blog/vuepress/assets/js/59.5d1d0ce6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/60.0b779f92.js"><link rel="prefetch" href="/blog/vuepress/assets/js/61.4ec3f2eb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/62.8f70f114.js"><link rel="prefetch" href="/blog/vuepress/assets/js/63.9ac2b61d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/64.c028fad5.js"><link rel="prefetch" href="/blog/vuepress/assets/js/65.923a8d15.js"><link rel="prefetch" href="/blog/vuepress/assets/js/66.73db652e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/67.1a91446d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/68.e5a12a42.js"><link rel="prefetch" href="/blog/vuepress/assets/js/69.6ddcb882.js"><link rel="prefetch" href="/blog/vuepress/assets/js/70.ceb70d85.js"><link rel="prefetch" href="/blog/vuepress/assets/js/71.a804ef40.js"><link rel="prefetch" href="/blog/vuepress/assets/js/72.f1c4098c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/73.2f28b842.js"><link rel="prefetch" href="/blog/vuepress/assets/js/74.571c2ab1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/75.f365c2a1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/79.44d7f95c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/80.4ca072f1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/81.d9239a49.js"><link rel="prefetch" href="/blog/vuepress/assets/js/82.b09096b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/83.f4bc8a9b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/84.1ffe6969.js"><link rel="prefetch" href="/blog/vuepress/assets/js/85.44e980f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/86.c8579ac0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/87.cd49f324.js"><link rel="prefetch" href="/blog/vuepress/assets/js/88.3851b6ff.js"><link rel="prefetch" href="/blog/vuepress/assets/js/89.b05297be.js"><link rel="prefetch" href="/blog/vuepress/assets/js/90.c1e3263e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/91.b493a1bb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/92.80ab06fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/93.c6ca00b3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/94.e993d310.js"><link rel="prefetch" href="/blog/vuepress/assets/js/95.8e19a7ed.js">
    <link rel="stylesheet" href="/blog/vuepress/assets/css/0.styles.d4f29607.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><a href="/blog/vuepress/" class="home-link router-link-active"><img src="/blog/vuepress/hero.png" class="logo"><span class="site-name can-hide">
      Denzel
    </span></a><div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""><!----></div><nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link router-link-active">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header><div class="sidebar-mask"></div><div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link router-link-active">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav><ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>HTML</span><!----></p><ul class="sidebar-group-items"><li><a href="/blog/vuepress/html/" class="sidebar-link">目录</a></li><li><a href="/blog/vuepress/html/web_components.html" class="sidebar-link">浅尝 Web Components </a></li><li><a href="/blog/vuepress/html/canvas_video_green_screen_cutout.html" class="sidebar-link">canvas像素点操作 —— 视频绿幕抠图</a></li><li><a href="/blog/vuepress/html/canvas_pixel_pick.html" class="sidebar-link">canvas像素点获取 —— 拾色器、放大器</a></li><li><a href="/blog/vuepress/html/dialog_element.html" class="active sidebar-link">HTML5.2新标签 —— dialog </a></li></ul></div></li></ul></div><div class="page"><div class="content"><h1 id="html5-2新标签-——-dialog"><a href="#html5-2新标签-——-dialog" aria-hidden="true" class="header-anchor">#</a> HTML5.2新标签 —— dialog</h1><p><a href="https://denzel.netlify.com/html/dialog_element.html" target="_blank" rel="noopener noreferrer">原文地址</a></p><h2 id="前言"><a href="#前言" aria-hidden="true" class="header-anchor">#</a> 前言</h2><p>继HTML5新增了诸多新的标签之后，HTML5.2又引入了几个现代标签，其中就包括&lt;dialog&gt;标签。
下面跟大家一块学习一下这个标签。</p><h2 id="举个栗子"><a href="#举个栗子" aria-hidden="true" class="header-anchor">#</a> 举个栗子</h2><p>下来看一个最简单的例子↓：</p><div class="dialog-wrapper1" data-v-5341b37c><button data-v-5341b37c>弹一下</button><dialog id="dialog" data-v-5341b37c>
        这是一个native弹窗。
    </dialog></div><p>以上就是dialog的原生态样式，未添加任何额外样式。这在Chrome和Firefox里表现是一致的，它们各自的默认样式是这样的，基本一致——</p><p>Chrome：
<img src="http://pn4meizzc.bkt.clouddn.com/dialog002.png" alt>
Firefox:
<img src="http://pn4meizzc.bkt.clouddn.com/dialog001.png" alt></p><p>嫌丑？！如果给它加上瘦脸（去掉默认样式），加上美颜（css），再加上特效（js），一样可以十分性感😄。</p><h2 id="两个属性"><a href="#两个属性" aria-hidden="true" class="header-anchor">#</a> 两个属性</h2><h3 id="open"><a href="#open" aria-hidden="true" class="header-anchor">#</a> open</h3><p>该属性意味着该对话框是可见的，没有它，这个对话框就会隐藏起来，直到你使用JavaScript来显示它，其实就是给它加上了open属性。</p><h3 id="returnvalue"><a href="#returnvalue" aria-hidden="true" class="header-anchor">#</a> returnValue</h3><p>用来获取close时传入的参数，看下面。</p><h2 id="三个方法"><a href="#三个方法" aria-hidden="true" class="header-anchor">#</a> 三个方法</h2><h3 id="show"><a href="#show" aria-hidden="true" class="header-anchor">#</a> show()</h3><h3 id="showmodal"><a href="#showmodal" aria-hidden="true" class="header-anchor">#</a> showModal()</h3><p>两个方法相同点都是打开弹窗，即都会给dialog元素添加一个open属性。
不同点：</p><p>唯一区别就是show()会按照其在DOM流中的位置显示dialog，没有遮罩，而showModal()会出现遮罩，
并且自动做了按键监控，即点击esc键，弹窗会关闭</p><p>大多数情况下，我们会使用便利的showModal()方法来而不使用show()方法。</p><h3 id="close"><a href="#close" aria-hidden="true" class="header-anchor">#</a> close()</h3><p>会关闭弹窗，即会删除open属性，并且可以携带一个参数作为额外数据，传入的值可以通过dialog.returnValue获取。</p><h2 id="两个事件"><a href="#两个事件" aria-hidden="true" class="header-anchor">#</a> 两个事件</h2><h3 id="close-2"><a href="#close-2" aria-hidden="true" class="header-anchor">#</a> close</h3><p>当modal关闭的时候触发</p><h3 id="cancel"><a href="#cancel" aria-hidden="true" class="header-anchor">#</a> cancel</h3><p>当按下ESC关闭模态框的时候触发</p><p>在各事件的事件对象event.target里，同样可以看到close()方法传入的参数，即event.target。returnValue。</p><h2 id="一个伪元素"><a href="#一个伪元素" aria-hidden="true" class="header-anchor">#</a> 一个伪元素</h2><h3 id="backdrop"><a href="#backdrop" aria-hidden="true" class="header-anchor">#</a> ::backdrop</h3><p>是dialog伪元素，用来设置弹窗遮罩的样式，用法如下</p><pre class="language-css"><code><span class="token selector">dialog::backdrop</span> <span class="token punctuation">{</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> <span class="token function">rgba</span><span class="token punctuation">(</span>0, 0, 0, 0.4<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><p>浏览器对backdrop也有默认的样式。
chrome：
<img src="http://pn4meizzc.bkt.clouddn.com/blogdialog003.png" alt></p><h3 id="再举个栗子"><a href="#再举个栗子" aria-hidden="true" class="header-anchor">#</a> 再举个栗子</h3><div class="dialog-wrapper2" data-v-1b613e3d><button data-v-1b613e3d>弹一下</button><dialog data-v-1b613e3d><div class="dialog-header" data-v-1b613e3d><span class="title" data-v-1b613e3d>提示</span><span class="close js-close" data-v-1b613e3d>×</span></div><div class="dialog-content" data-v-1b613e3d>
            这是一段消息。<br data-v-1b613e3d>
            这是另一段消息。
        </div><div class="dialog-footer" data-v-1b613e3d><a href="javascript:;" class="btn js-confirm" data-v-1b613e3d>确定</a><a href="javascript:;" class="btn js-cancel" data-v-1b613e3d>取消</a></div></dialog></div><h3 id="部分代码"><a href="#部分代码" aria-hidden="true" class="header-anchor">#</a> 部分代码</h3><pre class="language-js"><code>methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">onShow</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">showModal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>dialog <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">.</span>dialog2<span class="token punctuation">;</span>
    <span class="token keyword">let</span> closeBtn <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.js-close'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> confirmBtn <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.js-confirm'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> cancelBtn <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.js-cancel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//按esc关闭弹窗，同时会触发close事件</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'cancel'</span><span class="token punctuation">,</span> e <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token comment">//不写这句也会关闭，这里主要是为了携带数据及演示监听cancel事件</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token string">'按esc关闭'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//关闭</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'close'</span><span class="token punctuation">,</span> e <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> returnValue <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span>returnValue<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span>returnValue <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
        returnValue <span class="token operator">!==</span> <span class="token string">''</span> <span class="token operator">&amp;&amp;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$notify</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
            title<span class="token punctuation">:</span> <span class="token string">'returnValue'</span><span class="token punctuation">,</span>
            message<span class="token punctuation">:</span> returnValue
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//点击遮罩关闭，事件注册在dialog上</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>event<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>event<span class="token punctuation">.</span>target <span class="token operator">===</span> <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//关闭，并携带数据</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token string">'点击了遮罩关闭'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//点×关闭</span>
    closeBtn<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">,</span> e <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token string">'点击了关闭'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//点确定</span>
    confirmBtn<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">,</span> e <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token string">'点击了确定'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//点取消</span>
    cancelBtn<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">,</span> e <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token string">'点击了取消'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><pre class="language-css"><code><span class="token comment">/*改变dialog样式*/</span>
<span class="token selector">dialog</span> <span class="token punctuation">{</span>
    <span class="token property">position</span><span class="token punctuation">:</span> fixed<span class="token punctuation">;</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
    <span class="token property">left</span><span class="token punctuation">:</span> 50%<span class="token punctuation">;</span>
    <span class="token property">top</span><span class="token punctuation">:</span> 50%<span class="token punctuation">;</span>
    <span class="token property">transform</span><span class="token punctuation">:</span> <span class="token function">translate</span><span class="token punctuation">(</span>-50%, -50%<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token property">min-width</span><span class="token punctuation">:</span> 300px<span class="token punctuation">;</span>
    <span class="token property">max-width</span><span class="token punctuation">:</span> 80%<span class="token punctuation">;</span>
    <span class="token property">border</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
    <span class="token property">box-shadow</span><span class="token punctuation">:</span> 1px 1px 1px <span class="token function">rgba</span><span class="token punctuation">(</span>0, 0, 0, .3<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">dialog[open]</span> <span class="token punctuation">{</span>
    <span class="token property">animation</span><span class="token punctuation">:</span> slide-up 0.4s ease-out<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/*改变遮罩样式*/</span>
<span class="token selector">dialog::backdrop</span> <span class="token punctuation">{</span>
    <span class="token property">background</span><span class="token punctuation">:</span> <span class="token function">rgba</span><span class="token punctuation">(</span>0, 0, 0, 0.5<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token atrule"><span class="token rule">@keyframes</span> slide-up</span> <span class="token punctuation">{</span>
    <span class="token selector">0%</span> <span class="token punctuation">{</span>
        <span class="token property">opacity</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
        <span class="token property">transform</span><span class="token punctuation">:</span> <span class="token function">translate</span><span class="token punctuation">(</span>-50%, -40%<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  
    <span class="token selector">100%</span> <span class="token punctuation">{</span>
        <span class="token property">opacity</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
        <span class="token property">transform</span><span class="token punctuation">:</span> <span class="token function">translate</span><span class="token punctuation">(</span>-50%, -50%<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h3 id="polyfill"><a href="#polyfill" aria-hidden="true" class="header-anchor">#</a> polyfill</h3><p>What？！浏览器不支持？手机端没反应？！！</p><p>木有关系。这里有一个<a href="https://github.com/GoogleChrome/dialog-polyfill" target="_blank" rel="noopener noreferrer">dialog-polyfill</a>，
通过少许的额外代码，依然可以实现实现上面的功能。
只要引入该js，js中在弹窗对象后面添加一句：</p><pre class="language-js"><code>dialogPolyfill<span class="token punctuation">.</span><span class="token function">registerDialog</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>dialog<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>再引入它的css(很简单)即可。有兴趣的同学可以试试，这里不再赘述~
不支持的浏览器再试下这个Demo↓：</p><h3 id="最后一个栗子"><a href="#最后一个栗子" aria-hidden="true" class="header-anchor">#</a> 最后一个栗子</h3><div class="dialog-wrapper3" data-v-746df6f5><button data-v-746df6f5>弹一下</button><dialog data-v-746df6f5><div class="dialog-header" data-v-746df6f5><span class="title" data-v-746df6f5>提示</span><span class="close js-close" data-v-746df6f5>×</span></div><div class="dialog-content" data-v-746df6f5>
            这是一段消息。<br data-v-746df6f5>
            这是另一段消息。
        </div><div class="dialog-footer" data-v-746df6f5><a href="javascript:;" class="btn js-confirm" data-v-746df6f5>确定</a><a href="javascript:;" class="btn js-cancel" data-v-746df6f5>取消</a></div></dialog></div><h2 id="参考资料"><a href="#参考资料" aria-hidden="true" class="header-anchor">#</a> 参考资料</h2><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog" target="_blank" rel="noopener noreferrer">&lt;dialog&gt;: The Dialog element</a></li><li><a href="https://codepen.io/keithjgrant/pen/eyMMVL" target="_blank" rel="noopener noreferrer">https://codepen.io/keithjgrant/pen/eyMMVL</a></li><li><a href="https://webdesign.tutsplus.com/tutorials/native-popups-and-modals-with-the-html5-dialog-element--cms-23876" target="_blank" rel="noopener noreferrer">Native Popups and Modals With the HTML5 “dialog” Element</a></li><li><a href="https://yq.aliyun.com/articles/374584" target="_blank" rel="noopener noreferrer">初探HTML5.x新特性《dialog》标签</a></li></ul></div><div class="wx-wrapper" data-v-0de8a4bf><p class="wx-txt" data-v-0de8a4bf>欢迎关注</p><img src="http://pn4meizzc.bkt.clouddn.com/wxqr.png" class="wx-img" data-v-0de8a4bf></div><div class="beg-wrapper" data-v-1f1af99c><a href="javascript:;" class="btn show" data-v-1f1af99c><img src="http://pn4meizzc.bkt.clouddn.com/blogshang_log.png" data-v-1f1af99c></a><p class="shang-txt" data-v-1f1af99c>赏不赏，看您心情</p><img src="http://pn4meizzc.bkt.clouddn.com/blogWechatIMG35.jpeg" class="shangImg" data-v-1f1af99c></div><!----><div class="content page-nav"><p class="inner"><span class="prev">
        ← <a href="/blog/vuepress/html/canvas_pixel_pick.html" class="prev">
          canvas像素点获取 —— 拾色器、放大器
        </a></span><!----></p></div></div></div></div>
    <script src="/blog/vuepress/assets/js/41.e1e0a3bf.js" defer></script><script src="/blog/vuepress/assets/js/78.02cdd59d.js" defer></script><script src="/blog/vuepress/assets/js/77.469aed17.js" defer></script><script src="/blog/vuepress/assets/js/76.0916a3cd.js" defer></script><script src="/blog/vuepress/assets/js/app.15770e34.js" defer></script>
  </body>
</html>
